﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PhotoList.Dal;

namespace PhotoList.Web
{
    public partial class Main : System.Web.UI.Page
    {
        protected string ThumbnailWidth = ConfigurationManager.AppSettings["ThumbnailWidth"];
        protected string ThumbnailHeight = ConfigurationManager.AppSettings["ThumbnailHeight"];
        protected string uGroup = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (CheckSession()&&CheckAuthority())
            {
                new QT.Basic.BasePage();
            }
        }

        protected bool CheckSession()
        {
            if (base.Session["QT_UID"]==null)
            {
                string myScript = "<script anguage='javascript' type='text/javascript'>window.parent.location.href='Default.aspx';</script>";
                this.Response.Write(myScript);
                this.Response.End();

                return false;
            }

            PhotoList.Dal.User uUser = new PhotoList.Dal.User(base.Session["QT_UID"].ToString());

            if (uUser.Uid == "")
            {
                string myScript = "<script anguage='javascript' type='text/javascript'>window.parent.location.href='../Default.aspx';</script>";
                this.Response.Write(myScript);
                this.Response.End();

                return false;
            }

            Page.Session["QT_UID"] = uUser.Uid.Trim();
            Page.Session["QT_PASS"] = uUser.Pass.Trim();
            uGroup = uUser.Group.Trim();

            return true;
        }

        protected bool CheckAuthority()
        {
            string Target = Request.QueryString["Target"];
            DBO.FuncStruct fs = new DBO.FuncStruct();
            fs = DBO.CheckFunc(Target);
            if (!string.IsNullOrEmpty(fs.group) || !string.IsNullOrEmpty(fs.user))
            {
                string[] Groups = fs.group.ToUpper().Split(',');
                string[] Users = fs.user.ToUpper().Split(',');
                bool IsAuthority = false;
                for (int i = 0; i < Groups.Length; i++)
                {
                    if (Groups[i] == uGroup.ToUpper()) IsAuthority = true;
                }
                for (int i = 0; i < Users.Length; i++)
                {
                    if (Users[i] == Page.Session["QT_UID"].ToString().ToUpper()) IsAuthority = true;
                }
                if (IsAuthority)
                {
                    return true;
                }

            }

            new QT.Basic.Tools().Alert("您没有对页面" + Target + "的操作权限", "default.aspx");
            return false;
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
        }
    }
}